package com.example.ruhun_verify

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.core.content.ContextCompat

class OperatorAgreementDialog private constructor(
        context: Context,
        private val config: Config
) : Dialog(context) {

    class Config {
        var title: String = "协议确认"
        var content: String = "使用前请您仔细阅读并同意以下协议《用户使用协议》、《隐私政策》和《运营商认证服务协议》。"
        var agreeText: String = "同意"
        var disagreeText: String = "拒绝"
        var termsKeyword: String = "《用户使用协议》"
        var privacyKeyword: String = "《隐私政策》"
        var carrierKeyword: String = "《运营商认证服务协议》"
        var onAgree: () -> Unit = {}
        var onDisagree: () -> Unit = {}
        var onTermsClick: () -> Unit = {}
        var onPrivacyClick: () -> Unit = {}
        var onCarrierClick: () -> Unit = {}
    }

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_operator_agreement, null)
        setContentView(view)
        window?.setBackgroundDrawableResource(R.drawable.bg_dialog_rounded)

        view.findViewById<TextView>(R.id.tv_title).text = config.title
        view.findViewById<Button>(R.id.btn_agree).apply {
            text = config.agreeText
            background = ContextCompat.getDrawable(context, R.drawable.bg_button_rounded)
        }
        view.findViewById<Button>(R.id.btn_disagree).apply {
            text = config.disagreeText
            background = ContextCompat.getDrawable(context, R.drawable.bg_button2_rounded)
        }

        val contentTextView = view.findViewById<TextView>(R.id.tv_content).apply {
            movementMethod = LinkMovementMethod.getInstance()
            highlightColor = Color.TRANSPARENT
        }

        val spannable = SpannableString(config.content).apply {
            val termsStart = config.content.indexOf(config.termsKeyword)
            val termsEnd = termsStart + config.termsKeyword.length
            setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) = config.onTermsClick()
                override fun updateDrawState(ds: TextPaint) {
                    ds.color = ContextCompat.getColor(context, R.color.red_600)
                    ds.isFakeBoldText = true
                    ds.isUnderlineText = false // 关键修改：去除下划线
                }
            }, termsStart, termsEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

            val privacyStart = config.content.indexOf(config.privacyKeyword)
            val privacyEnd = privacyStart + config.privacyKeyword.length
            setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) = config.onPrivacyClick()
                override fun updateDrawState(ds: TextPaint) {
                    ds.color = ContextCompat.getColor(context, R.color.red_600)
                    ds.isFakeBoldText = true
                    ds.isUnderlineText = false // 关键修改：去除下划线
                }
            }, privacyStart, privacyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

            val carrierStart = config.content.indexOf(config.carrierKeyword)
            val carrierEnd = carrierStart + config.carrierKeyword.length
            setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) = config.onCarrierClick()
                override fun updateDrawState(ds: TextPaint) {
                    ds.color = ContextCompat.getColor(context, R.color.red_600)
                    ds.isFakeBoldText = true
                    ds.isUnderlineText = false // 关键修改：去除下划线
                }
            }, carrierStart, carrierEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }

        contentTextView.text = spannable

        view.findViewById<Button>(R.id.btn_agree).setOnClickListener {
            dismiss()
            config.onAgree()
        }
        view.findViewById<Button>(R.id.btn_disagree).setOnClickListener {
            dismiss()
            config.onDisagree()
        }
    }

    companion object {
        fun show(
                context: Context,
                block: Config.() -> Unit
        ): OperatorAgreementDialog {
            return OperatorAgreementDialog(context, Config().apply(block)).apply {
                show()
            }
        }
    }
}
